package com.vsd.service.impl;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.mobile.MobException;
import com.demo.mobile.MobileInfo;
import com.demo.util.SqlUtils;
import com.demo.util.StringUtil;
import com.vsd.domain.ChangeLog;
import com.vsd.domain.CreditGoods;
import com.vsd.domain.User;
import com.vsd.domain.UserCreditLog;
import com.vsd.mobile.error.MEnumError;
import com.vsd.repository.ChangeLogRepository;
import com.vsd.repository.CreditGoodsRepository;
import com.vsd.repository.UserCreditLogRepository;
import com.vsd.repository.UserRepository;
import com.vsd.service.CreditGoodsService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Transactional
public class CreditGoodsServiceImpl implements CreditGoodsService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(CreditGoodsServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	@Autowired
	private CreditGoodsRepository creditGoodsRepository;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private UserCreditLogRepository userCreditLogRepository;
	@Autowired
	private ChangeLogRepository changeLogRepository;

	@Override
	public Page<CreditGoods> findAll(Pageable p) {
		// TODO Auto-generated method stub
		return creditGoodsRepository.findAll(p);
	}

	@Override
	public Page<CreditGoods> findByCateCode(String code, Pageable p) {
		// TODO Auto-generated method stub
		return creditGoodsRepository.findByCateCode(code, p);
	}
	
	@Override
	public Page<CreditGoods> findByCateCodeLike(String code, Pageable p) {
		// TODO Auto-generated method stub
		return creditGoodsRepository.findByCateCodeLike(code, p);
	}

	@Override
	public CreditGoods findOne(String id) {
		// TODO Auto-generated method stub
		return creditGoodsRepository.findOne(id);
	}

	@Override
	public void changeCreditGoods(MobileInfo user,String id) throws MobException{
		
		CreditGoods goods = findOne(id);
		if(goods==null) throw new MobException(MEnumError.CREDIT_GOODS_NOT_FOUND);
		if(goods.getTotal()<=0) throw new MobException(MEnumError.CREDIT_GOODS_NOT_FOUND);
		User u = userRepository.findOne(user.getUserid());
		if(u.getCredit()<goods.getNum()) throw new MobException(MEnumError.CREDIT_NOT_ENOUGH);
		
		//扣除积分
		String sql = "update f_user u set u.credit = u.credit-"+goods.getNum()+" where u.id="+user.getUserid() +" and u.credit >= "+goods.getNum();
		int count = jdbcTemplate.update(sql);
		if(count>0){
			//保存积分日志
			UserCreditLog log = new UserCreditLog();
			log.setId(StringUtil.getUUID());
			log.setCreateTime(new Date());
			log.setFid(goods.getId());
			log.setInfo("兑换积分商品："+goods.getTitle());
			log.setItem("兑换积分商品："+goods.getTitle());
			log.setNum(goods.getNum());
			log.setTotal(u.getCredit()); //更新前数量
			log.setType(2);
			log.setUserId(user.getUserid());
			userCreditLogRepository.save(log);
			//更新积分商品库存
			sql="update s_credit_goods g set g.total = g.total - 1 where g.id = "+id;
			jdbcTemplate.equals(sql);
			//生成兑奖记录
			ChangeLog cLog = new ChangeLog();
			cLog.setId(StringUtil.getUUID());
			cLog.setCreateTime(new Date());
			cLog.setFid(id);
			cLog.setState(1);
			cLog.setType(1);
			cLog.setUserId(user.getUserid());
			changeLogRepository.save(cLog);
		}
		
	}

	@Override
	public int updateByPrimaryKeySelective(CreditGoods goods) {
		// TODO Auto-generated method stub
		try {
			String sql = SqlUtils.update(goods, true, " where id = '"+goods.getId()+"'");
			return jdbcTemplate.update(sql);
		}catch (Exception e) {
			return 0;
		}
		
		
	}

	@Override
	public void save(CreditGoods goods) {
		// TODO Auto-generated method stub
		creditGoodsRepository.save(goods);
	}

	@Override
	public void deleteById(String id) {
		// TODO Auto-generated method stub
		creditGoodsRepository.delete(id);
	}
	
 
 
 

	
}
